<?php

class Category_model extends Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::Model();
			
		$this->_table = $this->config->item('database_tables');
	}

	// Public methods
	public function change_status_category($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update($this->_table['category'],$data);		
	}
	public function get_categories($lang_id=null)
	{
		$this->db->select('id, name, url_name, description, title,status,parent_id');
		if($lang_id != null)
		{
			$this->db->where('lang_id',$lang_id);	
		}
		$query = $this->db->get($this->_table['category']);
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	
	public function get_categories_by_ids($category_ids)
	{
		$this->db->select('id, name, url_name, description');
		$this->db->where_in('id', $category_ids);
			
		$query = $this->db->get($this->_table['category']);
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}

	public function get_category($id)
	{
		$this->db->select('*');
		$this->db->where('id', $id);
			
		$query = $this->db->get($this->_table['category'], 1);
			
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
	}

	public function create_category($lang_id=null)
	{
		$parentObj = $this->get_category($this->input->post('parent_category'));
		$level = 0;
		if($parentObj != null)
		{
			$level = $parentObj['level'] + 1;
		}
		$data = array
					(
						'name' => $this->input->post('name'),
						'url_name' => url_title($this->input->post('url_name')),
						'description' => $this->input->post('description'),
						'title' => $this->input->post('title'),
						'lang_id'=>($lang_id==null)?2:$lang_id,
						'parent_id'=>$this->input->post('parent_category'),
						'level'=>$level
					);

		$this->db->insert($this->_table['category'], $data);
	}

	public function edit_category()
	{
		$parentObj = $this->get_category($this->input->post('parent_category'));
		$level = 0;
		if($parentObj != null)
		{
			$level = $parentObj['level'] + 1;
		}
		$data = array
					(
						'name' => $this->input->post('name'),
						'url_name' => $this->input->post('url_name'),
						'description' => $this->input->post('description'),
						'title' => $this->input->post('title'),
						'parent_id'=>$this->input->post('parent_category'),
						'level'=>$level
					);

		$this->db->where('id', $this->input->post('id'));
		$this->db->update($this->_table['category'], $data);
	}

	public function get_parent_category($id)
	{
		$this->db->select('*');
		$this->db->where('parent_id', $id);
			
		$query = $this->db->get($this->_table['category'], 1);
			
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		return null;
	}
	public function delete_category($id)
	{
		$this->db->where('id', $id);
			
		$this->db->delete($this->_table['category']);
	}
}

/* End of file categories_model.php */
/* Location: ./application/modules/admin/models/categories_model.php */